<script type="text/javascript">
$(function(){
	var pg = App.office.meetingEdit.ui, pp = pg.params || {};

	var frm = {
		title: pg.seek('title'),
		type: pg.seek('type'),
		priority: pg.seek('priority'),
		memo: pg.seek('memo'),
		startDate: pg.seek('startDate'),
		endDate: pg.seek('endDate'),
		startHour: pg.seek('startHour'),
		endHour: pg.seek('endHour'),
		startMin: pg.seek('startMin'),
		endMin: pg.seek('endMin'),
		owner: pg.seek('owner'),
		users: pg.seek('users'),
		doc: pg.seek('doc'),
		url: pg.seek('url'),
		status: pg.seek('status')
	};
	var dp = {
		prevText: "<上一月",
		nextText: "下一月>",
		dateFormat: "yy-mm-dd",
		changeMonth: true,
		changeYear: true,
		inline: true
	}
	frm.startDate.datepicker(dp);
	frm.endDate.datepicker(dp);
	
	frm.type.mac('combo', {
		width: 121,
		boxWidth: 122,
		readOnly: 1,
		data: Cache.office.meeting.type
	});
	frm.status.mac('combo', {
		width: 121,
		boxWidth: 122,
		readOnly: 1,
		data: Cache.office.meeting.status
	}).disable().val(0);
	
	var userGd = $('<div class="grid"></div>').mac('grid', {
		key: 'id',
		cols : [{
			field: 'code', title : '编号', width: 120
		},{
			field: 'name', title : '姓名', width: 240
		}],
		loader: {
			url: App.auth.ctx + '/sys/user/list.do',
			params: { pageNo: 1, pageSize: 50 },
			autoLoad: true
		},
		pagerLength: 10
	});
	frm.users.mac('combo', {
		keyField: 'id',
		displayField: 'name',
		multiSelect: true,
		width: 474,
		boxWidth: 430,
		boxHeight: 300,
		boxEl: userGd,
		onSelect: function(me, sk, sd){
			//var r = sd[0];
			//frm.users.text(r ? r.name : '');
		}
	});
	frm.users.mac('combo_grid', {});

	var ownerGd = $('<div class="grid"></div>').mac('grid', {
		key: 'id',
		cols : [{
			field: 'code', title : '编号', width: 120
		},{
			field: 'name', title : '姓名', width: 240
		}],
		loader: {
			url: App.auth.ctx + '/sys/user/list.do',
			params: { pageNo: 1, pageSize: 50 },
			autoLoad: true
		},
		pagerLength: 10
	});
	frm.owner.mac('combo', {
		keyField: 'id',
		displayField: 'name',
		multiSelect: false,
		width: 121,
		boxWidth: 400,
		boxHeight: 300,
		boxEl: ownerGd,
		onSelect: function(me, sk, sd){
			//var r = sd[0];
			//frm.owner.text(r ? r.name : '');
		}
	});
	frm.owner.mac('combo_grid', {});

	function getValues(){
		var st = new Date(frm.startDate.val());
		st.setHours(frm.startHour.val());
		st.setMinutes(frm.startMin.val());
		var et = new Date(frm.endDate.val());
		et.setHours(frm.endHour.val());
		et.setMinutes(frm.endMin.val());
		return {
			id: pp.id,
			parentId: pp.parentId,
			title: frm.title.val(),
			type: frm.type.val(),
			priority: frm.priority.val(),
			memo: frm.memo.val(),
			startTime: st.format('yyyy-MM-dd HH:mm:00'),
			endTime: et.format('yyyy-MM-dd HH:mm:00'),
			ownerId: frm.owner.val(),
			userIds: frm.users.val().join(','),
			url: frm.url.val(),
			status: frm.status.val(),
			flowId: pp.flowId,
			flowExeId: pp.flowExeId
		};
	}
	function setValues(o){
		var st = new Date(o.startTime);
		var et = new Date(o.endTime);
		pg.seek('title').html(o.id? '编辑组件':'添加组件');
		frm.title.val(o.title);
		frm.type.val(o.type);
		frm.priority.val(o.priority * 1);
		frm.memo.val(o.memo);
		frm.startDate.val(st.format('yyyy-MM-dd'));
		frm.endDate.val(et.format('yyyy-MM-dd'));
		frm.startHour.val(st.getHours());
		frm.endHour.val(et.getHours());
		frm.startMin.val(st.getMinutes());
		frm.endMin.val(et.getMinutes());
		frm.owner.val(o.owner);
		frm.users.val(o.users);
		frm.doc.attr('href', o.url);
		frm.url.val(o.url);
		frm.status.val(o.status || 0);
	}
	function init(d){
		setValues(d);
		var s = d.status, S = Cache.office.meeting;
		// 状态为草稿时可以提交（启动流程流转）
		if (s == S.STATUS_DRAFT) {
			pg.seek('submit').click(function(){
				var pms = getValues();
				if (!validate(pms)) {
					return;
				}
				mac.confirm('确定要启动此会议吗？', function(){
					$.post(App.office.ctx + '/meeting/submit.do', pms, function(rs){
						var ro = mac.eval(rs);
						if (ro.success) {
							pg.close();
							var ui = ro.data;
							if (ui.code) {
								App.go(ui.code);
							}
							return;
						}
						mac.alert(ro.data, ro.params);
					});
				});
				return false;
			}).show();
		}
		// 状态未排期、已取消、已完成时可以关闭
		if (s == S.STATUS_PENDING || s == S.STATUS_CANCEL || s == S.STATUS_DONE) {
			pg.seek('close').click(function(){
				mac.confirm('确定要关闭此会议吗？关闭后此会议将不再显示！', function(){
					$.get(App.office.ctx + '/meeting/close.do', {
						id: pp.id
					}, function(rs){
						var ro = mac.eval(rs);
						if (ro.success) {
							pg.close();
							return;
						}
						mac.alert(ro.data, ro.params);
					});
				});
				return false;
			}).show();
		}
		// 状态为未启动、进行中时可以取消
		if (s == S.STATUS_PENDING || s == S.STATUS_RUNNING) {
			pg.seek('cancel').click(function(){
				mac.confirm('确定要取消此会议吗？', function(){
					$.get(App.office.ctx + '/meeting/cancel.do', {
						id: pp.id
					}, function(rs){
						var ro = mac.eval(rs);
						if (ro.success) {
							pg.close();
							return;
						}
						mac.alert(ro.data, ro.params);
					});
				});
				return false;
			}).show();
		}
		// 状态为计划中时可以提交会议结果
		if (s == S.STATUS_SCHEDULED) {
			pg.seek('complete').click(function(){
				var cb = function(result){
					pg.close();
					var pms = getValues();
					if (!validate(pms)) {
						return;
					}
					pms.result = result; //结论
					$.post(App.office.ctx + '/meeting/complete.do', pms, function(rs){
						var ro = mac.eval(rs);
						if (ro.success) {
							var ui = ro.data;
							if (ui.code) {
								App.go(ui.code);
							}
							return;
						}
						mac.alert(ro.data, ro.params);
					});
				}
				mac.confirm('确定要完成此会议吗？', cb, {}, [{
					text: '通过',
					click: function(d) {
						d.close();
						cb(1);
					}
				}, {
					text: '不通过',
					click: function(d) {
						d.close();
						cb(2);
					}
				}, {
					text: '取消',
					click: function(d) {
						d.close();
					}
				}]);
				return false;
			}).show();
		}
	}
	
	function validate(p){
		var x = App.validate1, v = App.office.validation, m = pg.seek('msg');
		if (p.startTime && p.startTime > p.endTime) {
			m.text('结束时间不得早于开始时间');
			return;
		}
		var b = x(v.meeting.title, frm.title, m, '会议名') && x(v.meeting.ownerId, frm.owner, m, '发起人')
			&& x(v.meeting.type, frm.type, m, '会议类型') && x(v.meeting.priority, frm.priority, m, '优先级');
		return b;
	}
	pg.seek('ok').click(function(){
		var pms = getValues();
		if (!validate(pms)) {
			return;
		}
		$.post(App.office.ctx + '/meeting/save.do', pms, function(rs){
			var ro = mac.eval(rs);
			if (ro.success) {
				if (pg.callback) {
					pg.callback();
				}
				pg.close();
				return;
			}
			pg.seek('msg').html(mac.msg(ro.data, ro.params));
		});
		return false;
	});
	pg.seek('back').click(function(){
		pg.close();
	});

	if (pp.id) {
		$.get(App.office.ctx + '/meeting/get.do', {
			id: pp.id
		}, function(rs){
			var ro = mac.eval(rs);
			if (ro.success) {
				init(ro.data);
				return;
			}
			mac.alert(ro.data, ro.params);
		});
	} else {
		setValues({
			owner: App.logon,
			priority: 3,
			status: 0
		});
	}
});
</script>
<div class="form2">
	<div class="item w2">
		<div class="label">会议名</div>
		<input type="text" name="title" maxlength="128" minlength="8" />
		<span class="msg">*</span>
		<div class="clear"></div>
	</div>
	<div class="item">
		<div class="label">会议类型</div>
		<div class="combo fl" name="type"></div>
		<span class="msg">*</span>
		<div class="clear"></div>
	</div>
	<div class="clear"></div>
	<div class="item w3">
		<div class="label">参与人</div>
		<div class="combo fl" name="users"></div>
		<span class="msg">*</span>
		<div class="clear"></div>
	</div>
	<div class="clear"></div>
	<div class="item w2">
		<div class="label">
			<a href="#" target="_blank" name="doc">文档链接（点击前往）</a>
		</div>
		<input type="text" name="url" maxlength="1024" />
		<div class="clear"></div>
	</div>
	<div class="item">
		<div class="label">发起人</div>
		<div class="combo fl" name="owner"></div>
		<span class="msg">*</span>
		<div class="clear"></div>
	</div>
	<div class="clear"></div>
	<div class="item">
		<div class="label">开始日期</div>
		<input type="text" name="startDate" readOnly="true" />
		<div class="clear"></div>
	</div>
	<div class="item">
		<div class="label">结束日期</div>
		<input type="text" name="endDate" readOnly="true" />
		<div class="clear"></div>
	</div>
	<div class="item">
		<div class="label">状态：</div>
		<div class="combo fl" name="status" readOnly="true"></div>
		<div class="clear"></div>
	</div>
	<div class="clear"></div>
	<div class="item">
		<div class="label">开始时间</div>
		<input type="number" name="startHour" min="0" max="23" />
		<span>:</span>
		<input type="number" name="startMin" min="0" max="55" step="5" />
		<div class="clear"></div>
	</div>
	<div class="item">
		<div class="label">结束时间</div>
		<input type="number" name="endHour" min="0" max="23" />
		<span>:</span>
		<input type="number" name="endMin" min="0" max="55" step="5" />
		<div class="clear"></div>
	</div>
	<div class="item">
		<div class="label">优先级</div>
		<input type="number" name="priority" min="1" max="5" step="1" value="3" />
		<span class="msg">*</span>
		<div class="clear"></div>
	</div>
	<div class="clear"></div>
	<div class="item">
		<div class="msg" name="msg"></div>
		<div class="clear"></div>
	</div>
	<div class="clear"></div>
	<div class="bottom">
		<button name="submit" class="hidden">提交会议</button>
		<button name="cancel" class="hidden">取消会议</button>
		<button name="close" class="hidden">关闭会议</button>
		<button name="complete" class="hidden">结束会议</button>
		<button name="ok">保存</button>
		<button name="back">返回</button>
	</div>
</div>